package it.truestoryfactory.tsq.services.msg;

import it.truestoryfactory.tsq.game.util.Quiz;
import it.truestoryfactory.tsq.game.util.TableMacIp;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TSMHandler extends DefaultHandler {
	@SuppressWarnings("unused")
	private boolean _inRoot;
	private boolean _inDeviceAddress;
	private boolean _inIpAddress;
	@SuppressWarnings("unused")
	private boolean _inTableMacIp;
	@SuppressWarnings("unused")
	private boolean _inQuiz;
	@SuppressWarnings("unused")
	private boolean _inTopic;
	private boolean _inQuestionNumber;
	private boolean _inTime;
	
	private TSMessage msg;
	private int topicIndex;
	private int tableIndexMac;
	private int tableIndexIp;
	private int questionIndex;

	
	private TableMacIp tempTableMacIp;
	private Quiz tempQuiz;
	
	public TSMHandler() {
		msg = null;
		topicIndex = 0;
		tableIndexMac = 0;
		tableIndexIp = 0;
		questionIndex = 0;
	}
	
	public TSMessage getMessage() {
		return msg;
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		String chars = new String(ch, start, length); 
	    chars = chars.trim();
		if (_inDeviceAddress){
			if (msg instanceof JoinMessage)
				((JoinMessage) msg).setDeviceAddress(chars);
			else if (msg instanceof SetupMessage)
				tempTableMacIp.setMacByIndex(tableIndexMac++, chars);
		}else if (_inIpAddress){
			if (msg instanceof JoinMessage)
				((JoinMessage)msg).setIpAddress(chars);
			else if (msg instanceof SetupMessage)
				tempTableMacIp.setIpByIndex(tableIndexIp++, chars);
			else if (msg instanceof AnswerMessage)
				((AnswerMessage)msg).setIpAddress(chars);
		}else if (_inQuestionNumber){
			tempQuiz.setQuestionNumber(topicIndex, questionIndex++, Integer.valueOf(chars));
		}
		else if (_inTime) 
			((AnswerMessage) msg).setTime(Long.valueOf(chars));
			
		
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if (localName.equals(TSMessage.START_TAG)) 
			_inRoot=false;
		else if (localName.equals(JoinMessage.DEVICE_ADDRESS_TAG))
			_inDeviceAddress=false;
		else if (localName.equals(JoinMessage.IP_ADDRESS_TAG))
			_inIpAddress=false;
		else if (localName.equals(SetupMessage.TABLE_MAC_IP_TAG)){
			_inTableMacIp = false;
			((SetupMessage)msg).setTableMacIp(tempTableMacIp);
		}
		else if (localName.equals(SetupMessage.QUIZ_TAG)){
			_inQuiz = false;
			((SetupMessage)msg).setQuiz(tempQuiz);
		}
		else if (localName.equals(SetupMessage.TOPIC_TAG)){
			_inTopic = false;
			topicIndex++;
			questionIndex = 0;
		}
		else if (localName.equals(SetupMessage.QUESTION_NUMBER_TAG))
			_inQuestionNumber = false;
		else if (localName.equals(AnswerMessage.TIME_TAG))
			_inTime = false;
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		
		if (localName.equals(TSMessage.START_TAG)) {
			_inRoot = true;
			msg= TSMessage.newTSMessage(attributes.getValue(TSMessage.TYPE_ATTRIBUTE));
		}else if (localName.equals(JoinMessage.DEVICE_ADDRESS_TAG))
			_inDeviceAddress = true;
		else if (localName.equals(JoinMessage.IP_ADDRESS_TAG))
			_inIpAddress = true;
		else if (localName.equals(SetupMessage.TABLE_MAC_IP_TAG)){
			_inTableMacIp = true;
			tempTableMacIp = new TableMacIp(Integer.valueOf(attributes.getValue(SetupMessage.SIZE_ATTRIBUTE)));
		}else if (localName.equals(SetupMessage.QUIZ_TAG)){
			_inQuiz = true;
			tempQuiz = new Quiz(Integer.valueOf(attributes.getValue(SetupMessage.N_TOPICS_ATTRIBUTE)), 
					Integer.valueOf(attributes.getValue(SetupMessage.QST_4_TOPIC_ATTRIBUTE)), 
					Integer.valueOf(attributes.getValue(SetupMessage.SEC_4_QST_ATTRIBUTE)));
		}else if (localName.equals(SetupMessage.TOPIC_TAG)){
			_inTopic = true;
			tempQuiz.setTopic(topicIndex, attributes.getValue(SetupMessage.TOPIC_CODE_ATTRIBUTE));
		}else if (localName.equals(SetupMessage.QUESTION_NUMBER_TAG))
			_inQuestionNumber = true;
		else if (localName.equals(AnswerMessage.TIME_TAG))
			_inTime = true;
		
		
	}
	
}